package site.xuqing.androidbase.net;

import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.X509TrustManager;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import site.xuqing.androidbase.net.util.InterceptorUtil;
import site.xuqing.androidbase.net.util.SSLSocketClient;

/**
 * @author xuqing
 * @Description TODO
 * @createTime 2021年12月13日 14:12:32
 */
public abstract class BaseRequest {
    private final OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(20, TimeUnit.SECONDS)
        .readTimeout(20, TimeUnit.SECONDS)
        .writeTimeout(20, TimeUnit.SECONDS)
        .addInterceptor(InterceptorUtil.logInterceptor())
        .addNetworkInterceptor(InterceptorUtil.headerInterceptor())
        .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) { }
            
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) { }
            
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        })
        .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
        .build();
    
    private final Retrofit retrofit = new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
        .baseUrl(getUrl())
        .client(client)
        .build();
    
    /**
     * 获取url
     *
     * @return url
     */
    protected abstract String getUrl();
    
    public Retrofit getRetrofit(boolean needInterceptor, Interceptor... interceptors) {
        if (!needInterceptor) {
            return retrofit;
        } else {
            OkHttpClient.Builder mClientBuilder = new OkHttpClient.Builder();
            for (Interceptor interceptor : interceptors) {
                mClientBuilder.addInterceptor(interceptor);
            }
            OkHttpClient mClient = mClientBuilder
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(InterceptorUtil.logInterceptor())
                .addNetworkInterceptor(InterceptorUtil.headerInterceptor())
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) { }
                    
                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) { }
                    
                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }
                })
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .build();
            return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .baseUrl(getUrl())
                .client(mClient)
                .build();
        }
    }
    
    public BaseApiService getApiService(Class<? extends BaseApiService> clazz,Interceptor ...interceptors){
        if (interceptors.length<=0){
            return getRetrofit(false).create(clazz);
        }
        return getRetrofit(true,interceptors).create(clazz);
    }
}
